<template>
  <div class="login-wrap">
    <el-form
      label-position="left"
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="0px"
      class="demo-ruleForm login-container"
    >
      <!-- label-position:改变表单位置-->
      <div class="logo">
        <img src="@/assets/images/软件工厂logo.png" alt="" />
        <h1 class="title">就业后台管理系统</h1>
      </div>
      <el-form-item prop="username">
        <el-input
          type="text"
          v-model="ruleForm.username"
          auto-complete="off"
          placeholder="请输入用户名"
          suffix-icon="el-icon-user"
        >
        </el-input>
      </el-form-item>

      <el-form-item prop="password">
        <el-input
          type="password"
          v-model="ruleForm.password"
          auto-complete="off"
          placeholder="密码"
          show-password
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-checkbox>记住登录状态登录</el-checkbox>
        <el-button type="text" class="forgot">忘记密码？</el-button>
      </el-form-item>

      <el-form-item style="width: 100%">
        <el-button
          type="primary"
          style="width: 100%"
          class="log"
          @click.prevent="submitForm('ruleForm')"
          >登录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { login } from "@/api/index.js";
import { setToken } from "@/utils/token.js";
export default {
  name: "login",
  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      //rules前端验证
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    //获取info列表
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let res = await login(this.ruleForm);
          if (res.code == 200) {
            const token = res.data.satoken.tokenValue;
            const realname = res.data.realname;
            const rname = res.data.rname;
            localStorage.setItem("rname", JSON.stringify(res.data.rname));
            if (token) {
              setToken("token", token);
              setToken("name", realname);
              setToken("rname", rname);
              this.$router.push({ path: "/" });
              this.$message.success(`登录成功，欢迎您${res.data.rname}`);
            }
          } else {
            this.$message.error("登录失败");
          }
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style scoped>
.login-wrap {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  padding-top: 10%;
  background-image: url("@/assets/images/loginBackground.png");
  background-repeat: no-repeat;
  background-position: center right;
  background-size: 100%;
}

.login-container {
  border-radius: 10px;
  margin: 120px auto;
  width: 420px;
  height: 368px;
  padding: 30px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  text-align: left;
  box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
}
.logo {
  margin: auto;
  /* 水平居中 */
  text-align: center;
}

.title {
  margin: 0px auto 24px auto;
  text-align: center;
  /* 字体样式 */
  font-size: 18px;
  font-weight: normal;
  color: #505458;
}

/* 记住密码，忘记密码按钮 */
.forgot {
  /* 右对齐 */
  float: right;
  /* 字体颜色 */
  color: #000000;
}

.log {
  background-color: black;
}
</style>